package com.example.megaandroidrssreader;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;

public class SourcesActivity extends BaseActivity implements OnRssSourceModifyListener {

	public static final String SOURCES_XML_FILE_NAME = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "//sources.xml";

	protected ArrayList<RssSource> mRssSources;
	protected SourceListAdapter mSourceListAdapter;
	
	protected Boolean mSourceEditMode;

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		this.setContentView(R.layout.activity_rss_sources);

		mSourceEditMode = Boolean.FALSE;
		mRssSources = mRssReaderDao.getRssSources(); 
		mSourceListAdapter = new SourceListAdapter(this, R.layout.list_rss_source, mRssSources, mSourceEditMode);
		
		GridView listRssSources = (GridView) this.findViewById(R.id.listRssSources);
		listRssSources.setAdapter(mSourceListAdapter);
		listRssSources.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> adapter, View view, int position, long arg3) {
				Intent intent = new Intent(SourcesActivity.this.getApplicationContext(), ItemsActivity.class);
				intent.putExtra(BaseActivity.PACKAGE_NAME + "." + RssSource.RSS_SOURCE_TITLE, mRssSources.get(position).mTitle);
				intent.putExtra(BaseActivity.PACKAGE_NAME + "." + RssSource.RSS_SOURCE_URL, mRssSources.get(position).mUrl);
				view.getContext().startActivity(intent);
			}
			
		});
		
		Button button1 = (Button) this.findViewById(R.id.button1);
		Button button2 = (Button) this.findViewById(R.id.button2);
		
		button2.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				RssReaderDbHelper.copyDatabaseToExtSDCardDownloads(getApplicationContext());
			}
		
		});

		button1.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				RssReaderDbHelper dbHelper = RssReaderDbHelper.newInstance(getApplicationContext());
				dbHelper.onUpgrade(dbHelper.getWritableDatabase(), 1, 1);
			}
			
		});
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main_sources, menu);
		return true;
	}

	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		MenuItem editModeMenuItem = (MenuItem)menu.findItem(R.id.action_source_edit_mode);
		if (mSourceEditMode == Boolean.TRUE) {
			editModeMenuItem.setTitle("Edit Mode: On");
		} else {
			editModeMenuItem.setTitle("Edit Mode: OFF");
		}
		
		return super.onPrepareOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
			case R.id.action_source_edit_mode :
				if (mSourceEditMode == Boolean.TRUE) {
					mSourceEditMode = Boolean.FALSE;
					mSourceListAdapter.mEditMode = Boolean.FALSE;
				} else {
					mSourceEditMode = Boolean.TRUE;
					mSourceListAdapter.mEditMode = Boolean.TRUE;
				}
				mSourceListAdapter.notifyDataSetChanged();
				GridView listRssSources = (GridView) this.findViewById(R.id.listRssSources);
				listRssSources.refreshDrawableState();
				listRssSources.invalidateViews();
				
				this.invalidateOptionsMenu();
				return true;
			case R.id.action_add_source:
				SourceEditorFragment sourceEditorFragment = SourceEditorFragment.newInstance(true, null, 0);
				sourceEditorFragment.show(getFragmentManager(), "dialog");
			default:
				return super.onOptionsItemSelected(item);
		}
	}

	@Override
	public void onRssSourceModify(RssSource rssSource, int index, boolean isNew) {
		if (isNew) {
			if (mRssReaderDao.addRssSource(rssSource)) {
				this.mRssSources.add(rssSource);
			}
		} else {
			if (mRssReaderDao.updateRssSource(mRssSources.get(index), rssSource));
			mRssSources.set(index, rssSource);
		}
		this.mSourceListAdapter.notifyDataSetChanged();
	}
	
/*	private void loadSourcesFromXml() {
		File xmlFile = new File (SOURCES_XML_FILE_NAME);
	
		Document sourcesXml = null;
		try {
			sourcesXml = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(xmlFile);
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		}
	
		XPath xPath = XPathFactory.newInstance().newXPath();
		if (sourcesXml != null) {
			NodeList nodeList = null;
			try {
				nodeList = (NodeList) xPath.evaluate("/rss_sources/source", sourcesXml, XPathConstants.NODESET);
			} catch (XPathExpressionException e1) {
				e1.printStackTrace();
			}
			for (int i=0; i< nodeList.getLength(); i++) {
				Node item = nodeList.item(i);
				if (item.getNodeType() == Node.ELEMENT_NODE) {
					RssSource rssSource = new RssSource();
					try {
						rssSource.mTitle = xPath.evaluate("title", item);
						rssSource.mUrl = xPath.evaluate("url", item);
						mRssSources.add(rssSource);
					} catch (XPathExpressionException e) {
						e.printStackTrace();
					}
				}
			}
		}
	}*/

	
}
